﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WMSABL;
using ElectronicWMSA.Comm.ClassFiles;
using System.Xml;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.SY.SystemDocking
{
    public partial class WMSInterfaceFrm : Form
    {
        public WMSInterfaceFrm()
        {
            InitializeComponent();
            #region GetXML
            if (File.Exists("SytemConfig.xml"))
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(Application.StartupPath + "\\SytemConfig.xml");
                foreach (XmlNode item in xmldoc.GetElementsByTagName("DOC"))
                {
                    foreach (XmlNode items in item.ChildNodes)
                    {
                        if (items.Name == "SAPIP")
                            this.txtServerIP_SAP.Text = items.InnerText.Trim();
                        else if (items.Name == "SAPDB")
                            this.txtDataBaseName_SAP.Text = items.InnerText.Trim();
                        else if (items.Name == "SAPUSER")
                            this.txtUser_SAP.Text = items.InnerText.Trim();
                        else if (items.Name == "SAPPWD")
                        {
                            if (items.InnerText.Trim() != "")
                                this.txtPWD_SAP.Text = ElectronicWMSA.Comm.ClassFiles.PCInfoClass.Decryption(items.InnerText.Trim(), "/*-./.,@");
                            else
                                this.txtPWD_SAP.Text = "";
                        }
                    }
                }
            }
            #endregion
        }

        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }

        private void btnSQLConnection_Click(object sender, EventArgs e)
        {

            string ServerIP_SAP = this.txtServerIP_SAP.Text.Trim();
            string SAP_DBName = this.txtDataBaseName_SAP.Text.Trim();
            string SAP_user = this.txtUser_SAP.Text.Trim();
            string SAP_pwd = this.txtPWD_SAP.Text.Trim();
            try
            {
                #region
                if (ServerIP_SAP == "")
                {
                    MessageClass.error("不能为空！"); this.txtServerIP_SAP.Focus(); return;
                }
                if (SAP_DBName == "")
                {
                    MessageClass.error("不能为空！"); this.txtDataBaseName_SAP.Focus(); return;
                }
                if (SAP_user == "")
                {
                    MessageClass.error("不能为空！"); this.txtUser_SAP.Focus(); return;
                }
                if (SAP_pwd == "")
                {
                    MessageClass.error("不能为空！"); this.txtPWD_SAP.Focus(); return;
                }
                SAP_pwd = ElectronicWMSA.Comm.ClassFiles.PCInfoClass.Encryption(SAP_pwd, "/*-./.,@");
                #endregion

                if (MessageClass.enquire("保存设置参数！") == DialogResult.OK)
                {
                    XmlDocument doc = new XmlDocument();
                    string strFileName = Application.StartupPath + "\\SytemConfig.xml";
                    doc.Load(strFileName);
                    XmlNodeList nodeList = doc.SelectSingleNode("DOC").ChildNodes;
                    foreach (XmlNode xn in nodeList)
                    {
                        if (xn.Name == "SAPIP")
                            xn.InnerText = ServerIP_SAP;
                        else if (xn.Name == "SAPDB")
                            xn.InnerText = SAP_DBName;
                        else if (xn.Name == "SAPUSER")
                            xn.InnerText = SAP_user;
                        else if (xn.Name == "SAPPWD")
                            xn.InnerText = SAP_pwd;
                    }
                    doc.Save(strFileName);
                    if (MessageClass.enquire("设置成功，程序重启！") == DialogResult.OK)
                    {
                        Application.Exit();
                        System.Diagnostics.Process.Start(Application.ExecutablePath);
                    }
                }
            }
            catch (Exception)
            {
                MessageClass.error("错误！");
            }
        }
    }
}
